package top.mypath.util;

import cn.hutool.core.util.ReUtil;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import java.awt.*;
import java.util.regex.Pattern;

/**
 * @author LiYalin
 */
public class DoubleDocument extends PlainDocument {

    private static final String regexString="[1-9]\\d*\\.\\d*|0\\.\\d*\\d*|[1-9]\\d*|0?";

    public void insertString(int var1, String var2, AttributeSet var3) throws BadLocationException {
        if (this.isDouble(var2)) {
            super.insertString(var1, var2, var3);
        } else {
            Toolkit.getDefaultToolkit().beep();
        }

    }

    private boolean isDouble(String var1) {
        try {
            String text = this.getText(0, this.getLength());
            text=text+var1;
            return Pattern.compile(regexString).matcher(text).matches();
        }catch (BadLocationException e) {
            return false;
        }


    }



}
